package com.scut.dispatching_server.tool;

import java.util.ArrayList;

/**
 * OneToManyCalculation
 *
 * @author yuxi
 * @date 2018/12/3
 */
public class OneToManyCalculation {
    /**
     * @method getProbabilityDistribution 根据接收到目标订单的各司机对该订单在多对一情况下的响应时间概率分布，计算目标订单在多对多情况下响应时间的概率分布
     * @param driversProbabilityDistribution ArrayList<ArrayList<Float>> 接收到 targetOrder 的各司机对该订单在多对一情况下的响应时间概率分布
     * @return ArrayList<Float> 目标订单在多对多情况下响应时间的概率分布，以一秒为步长
     */
    public static float[] getProbabilityDistribution(ArrayList<float[]> driversProbabilityDistribution){
        float[] result = new float[driversProbabilityDistribution.get(0).length];
        for(int i = 0; i < driversProbabilityDistribution.get(0).length; i++){
            Float tmp = 1f;
            for(int j = 0; j < driversProbabilityDistribution.size(); j++){
                tmp = tmp * calculateBackProbability(i, driversProbabilityDistribution.get(j));
            }
            result[i] = 1 - calculateProbilityBeforeSecond(result, i) - tmp;
        }
        return result;
    };
    public static Float calculateBackProbability(Integer secondsLeft, float[] probabilityDistribution){
        Float probabilityLeft = (float)1;
        for(int k = 0; k <= secondsLeft; k++){
            probabilityLeft = probabilityLeft - probabilityDistribution[k];
        }
        return probabilityLeft;
    }
    public static Float calculateProbilityBeforeSecond(float[] probilityDistributionInMtm, int second){
        Float probabilityBeforeSecond = 0f;
        for(int secondIndex = 0; secondIndex < second; secondIndex++){
            probabilityBeforeSecond = probabilityBeforeSecond + probilityDistributionInMtm[secondIndex];
        }
        return probabilityBeforeSecond;
    }
}
